<?php
/**
 * 
 * 店铺信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-25
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/
class Store extends CActiveRecord {
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{Store}}';
	}
	
	/**
	 * 新增店铺
	 */
	public function addStore($addInfo){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{store}} (`store_name`,`store_info`,`store_type`,`store_years`,`store_owner`,`store_sort`,`active`) VALUES('".$addInfo['store_name']."','".$addInfo['store_info']."','".$addInfo['store_type']."','".$addInfo['store_years']."','".$addInfo['store_owner']."','".$addInfo['store_sort']."','".$addInfo['active']."')";
		$row = $cmodel -> upd($_sql);
		return $row;
	}
	/**
	 * 检测是否已经存在
	 */
public function checkExist($addInfo){
		$cmodel = new CCCModel();
		$result = $cmodel -> sel("SELECT * FROM {{store}} where store_name ='".$addInfo['store_name']."'");
		return $result;
	}
	
	/**
	 * 根据id查店铺信息
	 */
	public function getOneStore($store_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{store}} WHERE store_id=".$store_id;
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	/**
	 * 店铺修改
	 */
	public function StoreUpdate($recPost,$store_id){
		$cmodel = new CCCModel();
		
		if($recPost['send'] == "修改"){
			$_sql = "UPDATE {{store}} set 
							store_name='".$recPost['store_name']."',
							store_info='".$recPost['store_info']."',
							store_type='".$recPost['store_type']."',
							store_years=".$recPost['store_years'].",
							store_owner=".$recPost['store_owner'].",
							store_sort=".$recPost['store_sort'].",
							open=".$recPost['open']."
							WHERE store_id=".$store_id;
				$row = $cmodel->upd($_sql);
		}
		return $row;
		
	}

	/**
	 * 删除店铺
	 */
	public function storeDel($store_id){
		$cmodel = new CCCModel();
		$_sql = "DELETE FROM {{store}} WHERE store_id=".$store_id;
		$row = $cmodel -> upd($_sql);
		return $row;
	}
	/**
	 * 分页+搜索
	 */
	public function pageSearch($store_id,$type_name,$user_name){
		$cmodel = new CCCModel();
		$_sql = '';
		if($type_name == '管理员'){
		$_sql .= "SELECT * FROM {{store}}  WHERE  1=1 ";
		}elseif($type_name == '商家'){
			$_sql .= "SELECT * FROM {{store}}  WHERE  store_owner IN(SELECT user_id FROM {{user}} WHERE user_name='$user_name')";
		}else{
			echo "操作错误";
			exit;
		}
		$nextpage = "";
		if ($_POST){     
			if($_POST['active'] != 3){
				$nextpage .= "/active=".$_POST['active'];
				$_sql.=" AND active=".$_POST['active'];
			}
			elseif($_POST['keyword'] != null){
				$nextpage .= "&keyword=".$_POST['keyword'];
				$_sql.=" AND ( store_name like '%".$_POST['keyword']."%' OR store_info like '%".$_POST['keyword']."%')";
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
		else if ($store_id == 0){
			if($type_name == '管理员'){
				$_sql = "SELECT * FROM {{store}} ";
			}else{
				$_sql = "SELECT * FROM {{store}}  WHERE  store_owner IN(SELECT user_id FROM {{user}} WHERE user_name='$user_name')";
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
	
			/**
			 * 分页
			 */
			$per = 10;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per,$nextpage);
			$_sql .= " ORDER BY store_sort ASC $page->limit";
			$infos = $cmodel->sel($_sql);
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
			return $infos;
	}
	
	/**
	 * 根据用户名查询商铺
	 */
	public function getUserStore($user_name){
		$cmodel = new CCCModel();
		$_userInfo_sql = "SELECT role_name FROM {{role}} a WHERE a.role_id IN (SELECT role_id FROM {{user}} WHERE user_name='".$user_name."')";
		$userInfo = $cmodel->sel($_userInfo_sql);
		$rolename = $userInfo[0]['role_name'];
		if($rolename == "管理员"){
			$_sql = "SELECT * FROM {{store}}";
		}else{
			$_sql = "SELECT * FROM {{store}} WHERE store_owner IN(SELECT user_id FROM {{user}} WHERE user_name = '".$user_name."')";
		}
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	
	/**
	 * 查询有用者姓名
	 */
	public function getOwnName(){
		$all_model = new CCCModel();
		$_sql = "SELECT user_id,user_name FROM {{user}}";
		$result = $all_model->sel($_sql);
		return $result;
	}

	
	/**
	 * 按照sort排序
	 * @param unknown_type $_data
	 */
	public function sort($_data){
		$cmodel = new CCCModel();
		foreach($_data as $_k => $_v){
			$_sql = "UPDATE {{store}} SET store_sort=".$_v." WHERE store_id=".$_k;
			$a = $cmodel->upd($_sql);
		}
	}
}
	
?>